package com.beone.smart.gateway.config;


import com.beone.smart.gateway.result.ResultEntity;
import com.beone.smart.gateway.result.ResultTool;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Date: 2019/8/4
 * @Author: yangjie
 **/
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody //在返回自定义相应类的情况下必须有，这是@ControllerAdvice注解的规定
    public ResultEntity exceptionHandler(Exception exception) {
        exception.printStackTrace();
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setStatus(400);
        resultEntity.setMsg(exception.getMessage());
        return ResultTool.fail(resultEntity);
    }

    @ExceptionHandler(NotFoundException.class)
    public ResultEntity notFoundPage404(Exception exception) {
        exception.printStackTrace();
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setStatus(404);
        resultEntity.setMsg(exception.getMessage());
        return ResultTool.fail(resultEntity);
    }

}

